//
//  InviteViewController.m
//  leo
//
//  Created by 常艳 on 16/2/28.
//  Copyright © 2016年 mdx. All rights reserved.
//

#import "InviteViewController.h"
#import "Tooler.h"
#import "LeoMsg.h"
#import "SyncManager.h"
#import "User.h"
#import "InviteResultViewController.h"
#import "AppDelegate.h"
@interface InviteViewController ()

@end

@implementation InviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"添加副管理员";
    float x=self.phone.frame.size.height;
    UIView *phoneleftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, x)];
    self.phone.leftView=phoneleftView;
    self.phone.leftViewMode=UITextFieldViewModeAlways;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)invite:(id)sender {
    self.inviteBtn.enabled=NO;
    NSString *phone=[self.phone text];
    if(phone==NULL)
    {
        [LeoMsg showMsg:@"手机号码不能为空"];
        self.inviteBtn.enabled=YES;
        return;
    }
    phone=[phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if([phone length]==0)
    {
        NSLog(@"%@,getSmsCode,phone is null",Invite_tag);
        [LeoMsg showMsg:@"手机号码无效，请输入正确的手机号码"];
        self.inviteBtn.enabled=YES;
        return;
    }
    if([phone length]!=11||[Tooler checkStringBegin:phone]==NO)
    {
        NSLog(@"%@,getSmsCode,phone is null",Invite_tag);
        [LeoMsg showMsg:@"手机号码无效，请输入正确的手机号码"];
        self.inviteBtn.enabled=YES;
        return;
    }
    SyncManager *syncManager=[[SyncManager alloc] init];
    DataManager *dataManager=[[DataManager alloc] init];
    if(syncManager==NULL||dataManager==NULL)
    {
        NSLog(@"%@,getSmsCode,syncer is null",Invite_tag);
        [LeoMsg showMsg:@"程序发生错误，code is 26"];
        self.inviteBtn.enabled=YES;
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        self.inviteBtn.enabled=YES;
        return;
    }
    Watch *selectWatch=[dataManager getActiveWatch:user.uid];
    if(selectWatch==NULL)
    {
        self.inviteBtn.enabled=YES;
        return;
    }
    [syncManager inviteViceAdmin:selectWatch.deviceUid vaaccount:phone completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
            if(mydelegate!=NULL)
                mydelegate.isReceiveBindRelationChange=YES;
            int groupId=[dataManager getGroupid:selectWatch.deviceUid accountId:user.uid];
            NSArray *watchLists=[dataManager getGroupWatchList:groupId];
            if(watchLists!=NULL&&[watchLists count]>0)
            {
                for(int i=0;i<[watchLists count];i++)
                {
                    Watch *watch=[watchLists objectAtIndex:i];
                    if(watch==NULL||watch.deviceUid==NULL)
                        continue;
                    [syncManager getBoundUsers:watch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {

                    }];
                }
            }
            NSString *nickName=@"副管理员";
            int type=0;
            if(retValue!=NULL&&[retValue count]>0)
            {
                nickName=[retValue valueForKey:@"nickName"];
                type=[[retValue valueForKey:@"type"] intValue];
            }
            if(type==1)
            {
                InviteResultViewController *inviteResult=[[InviteResultViewController alloc] init];
                inviteResult.type=1;
                inviteResult.nickName=nickName;
                [self.navigationController pushViewController:inviteResult animated:YES];
            }
            else
            {
                InviteResultViewController *inviteResult=[[InviteResultViewController alloc] init];
                inviteResult.type=2;
                [self.navigationController pushViewController:inviteResult animated:YES];
            }

            
        }
        else
        {
            if(result==NODATA)
            {
                [LeoMsg showMsg:@"当前网络异常，请检查网络"];
            }
            else
            {
                if(description!=NULL)
                {
                    if([description isEqualToString:DEVICE_ALREADY_BOUND_INFO])
                        [LeoMsg showMsg:@"您已经添加了该副管理员"];
                    else
                        [LeoMsg showMsg:description];
                }
                else
                    [LeoMsg showMsg:@"邀请副管理员失败"];
            }
            self.inviteBtn.enabled=YES;
        }
    }];

}
- (IBAction)textFieldChange:(id)sender {
    if(self.phone.text.length>20)
        self.phone.text=[self.phone.text substringToIndex:20];
    self.phone.text=[Tooler validateNumber:self.phone.text];
    
}
- (IBAction)backgroundTaped:(id)sender {
    [self.phone resignFirstResponder];
}
@end
